40823129

  • Home
    • Site Map
    • reveal
    • blog
  • 簡介
  • About
  • Develop
  • 課程內容
    • W1
      • 建立網頁
    • W2
      • HW1
      • HW2
    • W3
    • W4
      • 如何建立ssh
    • W5
      • API
    • W6
    • W7
      • 工程師的特質
      • 利用 SciTE 編輯器類解譯 ANSI C 程式
    • W8
    • W9
    • W10
    • W11
    • W12
    • W13
    • W14
    • W15
    • W16
    • W17
    • W18
      • 期末報告
  • 課程筆記
    • 指令
    • proxy設定
工程師的特質 << Previous Next >> W8

利用 SciTE 編輯器類解譯 ANSI C 程式

下載 tcc-0.9.27-win64-bin.zip

解壓縮到 y:\ 根目錄中 

接著必須將 y:\tcc\ 放入隨身系統中的 path 命令搜尋路徑中, 一旦啟動後就可以直接執行 tcc.exe, 也就是 start.bat 必須修改如下:

1
2
3
4
5
set path_portablegit=%Disk%:\Portablegit\bin;
set path_julia=%Disk%:\julia-1.5.2\bin;
set path_tcc=%Disk%:\tcc\;
 
path=%Disk%:;%path_python%;%path_portablegit%;%path_julia%;%path_tcc%;%path%;

修改完後,利用 stop.bat 關閉隨身系統後, 再使用 start.bat 重新啟動

再使用 start.bat 重新啟動, 並在命令列中輸入 tcc, 若出現

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Tiny C Compiler 0.9.27 - Copyright (C) 2001-2006 Fabrice Bellard
Usage: tcc [options...] [-o outfile] [-c] infile(s)...
       tcc [options...] -run infile [arguments...]
General options:
  -c          compile only - generate an object file
  -o outfile  set output filename
  -run        run compiled source
  -fflag      set or reset (with 'no-' prefix) 'flag' (see tcc -hh)
  -Wwarning   set or reset (with 'no-' prefix) 'warning' (see tcc -hh)
  -w          disable all warnings
  -v -vv      show version, show search paths or loaded files
  -h -hh      show this, show more help
  -bench      show compilation statistics
  -           use stdin pipe as infile
  @listfile   read arguments from listfile
Preprocessor options:
  -Idir       add include path 'dir'
  -Dsym[=val] define 'sym' with value 'val'
  -Usym       undefine 'sym'
  -E          preprocess only
Linker options:
  -Ldir       add library path 'dir'
  -llib       link with dynamic or static library 'lib'
  -r          generate (relocatable) object file
  -shared     generate a shared library/dll
  -rdynamic   export all global symbols to dynamic linker
  -soname     set name for shared library to be used at runtime
  -Wl,-opt[=val]  set linker option (see tcc -hh)
Debugger options:
  -g          generate runtime debug info
  -b          compile with built-in memory and bounds checker (implies -g)
  -bt N       show N callers in stack traces
Misc. options:
  -x[c|a|n]   specify type of the next infile
  -nostdinc   do not use standard system include paths
  -nostdlib   do not link with standard crt and libraries
  -Bdir       set tcc's private include/library dir
  -MD         generate dependency file for make
  -MF file    specify dependency file name
  -m32/64     defer to i386/x86_64 cross compiler
Tools:
  create library  : tcc -ar [rcsv] lib.a files
  create def file : tcc -impdef lib.dll [-v] [-o lib.def]

表示啟動命令搜尋路徑已經包含 y:\tcc 目錄

接下來必須修改 SciTE 編輯器中的 cpp.properties, 讓 Tools -> Go 可以呼叫 tcc.exe 類解譯編輯器中副檔名為 .c 的 ANSI C 程式.

之後在編輯器中加入 hello.c 並且利用 Tools -> Go 直接執行.

1
2
3
4
int main() {
printf("Hello, world!\n");
return 0;
}

完成上述所有步驟設定後, 可以執行下列 ANSI C 程式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Runge Kutta for a set of first order differential equations */
  
#include <stdio.h>
#include <math.h>
  
#define N 2 /* number of first order equations */
#define dist 0.1 /* stepsize in t*/
#define MAX 30.0 /* max for t */
  
FILE *output; /* internal filename */
FILE *output1; /* internal filename */
// 利用 pipe 呼叫 gnuplot 繪圖
FILE *pipe;
  
void runge4(double x, double y[], double step); /* Runge-Kutta function */
double f(double x, double y[], int i); /* function for derivatives */
  
void main(){
  
  double t, y[N];
  int j;
  
  output=fopen("osc.dat", "w"); /* external filename */
  output1=fopen("osc1.dat", "w"); /* external filename */
  
  y[0]=1.0; /* initial position */
  y[1]=0.0; /* initial velocity */
  
  //fprintf(output, "0\t%f\n", y[0]);
  
  for (j=1; j*dist<=MAX ;j++) /* time loop */{
  
    t=j*dist;
    runge4(t, y, dist);
    fprintf(output, "%f\t%f\n", t, y[0]);
    fprintf(output1, "%f\t%f\n", t, y[1]);
  }
  
  fclose(output);
  fclose(output1);
  
  pipe = popen("gnuplot -persist","w");
  //fprintf(pipe,"set term png enhanced font \"v:/fireflysung.ttf\" 18 \n");
  fprintf(pipe,"set term png enhanced font \"y:/wqy-microhei.ttc\" 18 \n");
  //fprintf(pipe,"set yrange [68:70]\n");
  fprintf(pipe,"set output \"test.png\"\n");
  fprintf(pipe, "plot \"osc.dat\" title \"位移\" with lines, \"osc1.dat\" title \"速度\" with lines\n");
  fprintf(pipe,"quit\n");
 
  fprintf(pipe,"quit\n");
  pclose(pipe);
}
  
void runge4(double x, double y[], double step){
  
  double h=step/2.0, /* the midpoint */
  t1[N], t2[N], t3[N], /* temporary storage arrays */
  k1[N], k2[N], k3[N],k4[N]; /* for Runge-Kutta */
  int i;
  
  for (i=0;i<N;i++){
  
    t1[i]=y[i]+0.5*(k1[i]=step*f(x,y,i));
  }
  
  for (i=0;i<N;i++){
  
    t2[i]=y[i]+0.5*(k2[i]=step*f(x+h, t1, i));
  }
  
  for (i=0;i<N;i++){
  
    t3[i]=y[i]+ (k3[i]=step*f(x+h, t2, i));
  }
  
  for (i=0;i<N;i++){
  
    k4[i]= step*f(x+step, t3, i);
  }
  
  for (i=0;i<N;i++){
  
    y[i]+=(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
  }
}
  
double f(double x, double y[], int i){
  
  if (i==0)
    x=y[1]; /* derivative of first equation */
  if (i==1)
    x=-y[0]-0.5*y[1];
  return x;
}


工程師的特質 << Previous Next >> W8

Copyright © All rights reserved | This template is made with by Colorlib